
package doc2db.interfaces.action;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JEditorPane;

/**
 *
 * @author yuriy
 */
public class ShowTableAction extends CreateDialog implements GUIAction{//ActionListener{

    public ShowTableAction(JEditorPane __p, JEditorPane t2) {
        super(__p, t2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!Keey.isConnected) {
            return;
        }

        AbstractGUIAction.init(this);

        if (res.equals("__NULL")) {
            this.show();
        }
        if (res.equals("__NULL")) {
            return;
        }

        MyJEditorPane mP = (MyJEditorPane) super.p;
        mP.tableName = "" + super.res;

        String[][] result = null;
        String[] head = null;
        UserProp up = new UserProp();
        String str = "SELECT * FROM \"" + res + "\" ;";
        try {
            DbSelects query = new DbSelects(up.getURL(), up.getUserProp());
            if (query.sendQuery(str)) {
                int rows = query.getRows();
                int cols = query.getColumns();
                result = new String[rows][cols];
                head = new String[cols];
                for (int i = 0; i < head.length; i++) {
                    try {
                        head[i] = "" + query.getMetaData().getColumnName(i + 1);
                    } catch (SQLException exception) {
                    }
                }
                for (int i = 1; i <= rows; i++) {
                    for (int j = 1; j <= cols; j++) {
                        result[i - 1][j - 1] = "" + query.getString(i, j);
                    }
                }
                CreateHtmlFile chf = new CreateHtmlFile(p, result, head);
                chf.setPage();
                String s = "";
                //s += this.e.getText();
                this.e.setText(s + "\n\n" + str);
                MyJEditorPane np = (MyJEditorPane) p;
                np.isInTable = true;
            }
        } catch (Exception ex) {
            if (Keey.debug) {
                System.err.println(ex);
            }
        }
        res = "__NULL";
    }

    @Override
    public boolean start() {
        return true;
    }

    @Override
    public boolean end() {
        return true;
    }

    @Override
    public void action(ActionEvent e) {
    }

    @Override
    public String getMsg() {
        return null;
    }
}
